package com.example.helloworth;

import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.widget.TextView;

public class MonthlyBillActivity extends Activity {

	private TextView totalIncome;
	private TextView totalPay;
	private TextView amount;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_monthly_bill);
	
		//view
		totalIncome = (TextView) findViewById(R.id.textView3);
		totalPay = (TextView) findViewById(R.id.textView5);
		amount = (TextView) findViewById(R.id.textView7);
		
		//DB
		DatabaseHelper dh = new DatabaseHelper(MonthlyBillActivity.this, "bighusband_db");
		SQLiteDatabase  sqliteDatabase = dh.getReadableDatabase();
		String[] args1 = {"1"};
		String[] args2 = {"2"};
		String selectMoney = "select sum(money) from consumerrecord  where recordtype = ?";
		Cursor cursor1 = sqliteDatabase.rawQuery(selectMoney, args1);
		Cursor cursor2 = sqliteDatabase.rawQuery(selectMoney, args2);
			
		cursor1.moveToPosition(0);
 		int incomeColumnIndex = cursor1.getColumnIndex("sum(money)");
		String income =  cursor1.getString(incomeColumnIndex);
		
		cursor2.moveToPosition(0);
 		int payColumnIndex = cursor2.getColumnIndex("sum(money)");
		String pay =  cursor2.getString(payColumnIndex);
						
		//if(pay.equals("") || income.equals("")){
		if(pay == null && income == null){
			totalIncome.setText("0");
			totalPay.setText("0");
			amount.setText("0");
			return;
		}		
				
		totalIncome.setText(income);
		totalPay.setText(pay);
		int am = Integer.parseInt(income) - Integer.parseInt(pay);
		amount.setText(String.valueOf(am));
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_monthly_bill, menu);
		return true;
	}

}
